﻿Imports Tom0014
Imports Tom0014TableAdapters
Partial Class Registro_Sanidad
    Inherits System.Web.UI.Page
    Dim animales As New animalTableAdapter
    Dim sanidad As New SanidadTableAdapter
    Dim iAnimalId As Integer
    Dim idSan As Integer
    Dim sdsSanidad As New SqlDataSource
    Dim sSQL As String
    Dim categorias As New catalogo_generalDataTable
    Dim CategoriasAdap As New catalogo_generalTableAdapter
    Protected Sub Registro_Sanidad_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        categorias = CategoriasAdap.GetCatalogo()
        Try
            iAnimalId = CType(Request.QueryString("Animal"), Integer)
        Catch ex As Exception
            iAnimalId = 0
        End Try
        Dim animal = animales.GetAnimalById(iAnimalId)
        If animal.Count > 0 Then
            lblAnimal.Text = animal(0).claveSNLA + " " + animal(0).nombre
        End If
        ActualizaGrid()
        btnEditar.Enabled = False
        If Not IsPostBack Then
            ' Dim categoriaTable As New ponderacionesDataTable
            ' categoriaTable = ponderaciones.GetPonderacionByCategoria(Cadena)
            '  DropDown.DataSource = categoriaTable
            Dim categotias As New catalogo_generalDataTable
            categorias = CategoriasAdap.GetCatalogoByTipo("SA")
            ddlTiposSanidad.DataSource = categorias
            'ddlTiposSanidad.DataSource = categorias.Select("cattip='" + "SA" + "'", "catnom")
            ddlTiposSanidad.DataTextField = categorias.Columns("catnom").ToString
            ddlTiposSanidad.DataValueField = categorias.Columns("catcve").ToString
            ddlTiposSanidad.DataBind()
        End If
    End Sub
    Sub ActualizaGrid()
        GVSanidad.DataSource = sanidad.GetSanidad(iAnimalId)
        GVSanidad.DataBind()
    End Sub
    Protected Sub GVSanidad_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GVSanidad.SelectedIndexChanged
        Dim FechaSan As String = GVSanidad.SelectedRow.Cells(1).Text
        Dim TipoSan As String = GVSanidad.SelectedRow.Cells(2).Text
        Dim ProductoSan As String = GVSanidad.SelectedRow.Cells(3).Text
        Dim DosisSan As String = GVSanidad.SelectedRow.Cells(4).Text
        Dim ComentariosSan As String = GVSanidad.SelectedRow.Cells(5).Text
        idSan = GVSanidad.SelectedIndex
        idSan = GVSanidad.DataKeys(idSan).Item("id")
        ' Dim cadena As String
        'For Each Item In ddlTiposSanidad.Items
        '    cadena = Item.ToString()
        '    'cadena = ddlTiposSanidad.SelectedValue.ToString
        '    If cadena = TipoSan Then
        '        Item.selected = True
        '    Else
        '        Item.selected = False
        '    End If
        'Next
        ddlTiposSanidad.SelectedValue = TipoSan
        txtFechaTienta.Text = FechaSan
        tbProducto.Text = ProductoSan
        tbDosis.Text = DosisSan
        tbComentario.Text = ComentariosSan
        desActiva()
        BtnBorrar.Enabled = True
        btnNuevo.Enabled = True
    End Sub
    Protected Sub btnEditar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEditar.Click
        Dim fecha As String = Date.Today
        Editar()
    End Sub
    Sub Editar()
        ' ddlTiposSanidad.Enabled = True
        txtFechaTienta.Enabled = True
        tbProducto.Enabled = True
        tbDosis.Enabled = True
        tbComentario.Enabled = True
        btnEditar.Enabled = False
        BtnBorrar.Enabled = True
        BtnSalvar.Enabled = True
    End Sub
    Sub desActiva()
        'ddlTiposSanidad.Enabled = False
        txtFechaTienta.Enabled = False
        tbProducto.Enabled = False
        tbDosis.Enabled = False
        tbComentario.Enabled = False
        btnEditar.Enabled = True
        BtnBorrar.Enabled = False
        BtnSalvar.Enabled = False
    End Sub
    Sub inicia()
        txtFechaTienta.Text = Date.Today
        tbProducto.Text = ""
        tbDosis.Text = ""
        tbComentario.Text = ""
    End Sub
    Protected Sub btnNuevo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        Editar()
        inicia()
        BtnBorrar.Enabled = False
        btnNuevo.Enabled = False
    End Sub
    Protected Sub BtnSalvar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSalvar.Click
        If Len(txtFechaTienta.Text) < 1 Then
            Return
        End If
        Dim TipoSan As String
        Dim fecha As String
        fecha = txtFechaTienta.Text
        Dim iNumber As Integer = iAnimalId
        'Dim cadena1 As String
        Dim FechaSan As Date = Convert.ToDateTime(txtFechaTienta.Text)
        'cadena1 = Format(fecha, "General Date") ' Devuelve: "06/08/1978"
        TipoSan = ddlTiposSanidad.SelectedValue.ToString
        'Dim cadena As String
        ' For Each Item In ddlTiposSanidad.Items
        'cadena = ddlTiposSanidad.SelectedValue.ToString

        ' If Item.selected = True Then
        'TipoSan = cadena
        ' End If
        ' Next
        Dim ProductoSan As String = tbProducto.Text
        Dim DosisSan As String = tbDosis.Text
        Dim ComentariosSan As String = tbComentario.Text
        sSQL = "INSERT INTO Sanidad (AnimalId,  Fecha, tipoSanidad, Producto, Dosis, Comentarios) VALUES  ('" + iNumber.ToString + "'" & "," & "'" + FechaSan + "'" & "," & "'" + TipoSan + "'" & "," & "'" + ProductoSan + "'" & "," & "'" + DosisSan + "'" & "," & "'" + ComentariosSan + "')"
        Try
            sdsSanidad.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
            sdsSanidad.InsertCommand = sSQL
            sdsSanidad.Insert()
        Catch ex As Exception
            ' Response.Write(ex.Message)
        End Try
        inicia()
        desActiva()
        ActualizaGrid()
        btnEditar.Enabled = False
        btnNuevo.Enabled = True
    End Sub
    Protected Sub BtnBorrar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnBorrar.Click
        idSan = GVSanidad.SelectedIndex
        idSan = GVSanidad.DataKeys(idSan).Item("id")
        Dim iNumber As Integer = iAnimalId
        sSQL = "DELETE FROM Sanidad where AnimalId = " & iNumber & " and id = " & idSan
        Try
            sdsSanidad.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
            sdsSanidad.DeleteCommand = sSQL
            sdsSanidad.Delete()

        Catch ex As Exception
        End Try
        inicia()
        desActiva()
        ActualizaGrid()
        btnEditar.Enabled = False
    End Sub
End Class
